package msu.ml.data;

import java.io.*;

/**
 * The LittleEndianDataOutputStream extends DataOutputStream by adding
 * a layer that provides the ability to write various data type in little
 * endian data format.
 * @author Reginald M Mead
 * @version 1.0
 * @see DataOutputStream
 */
public class LittleEndianDataOutputStream extends DataOutputStream
{
	/** Creates a new little endian data output stream for 
	 * writing little endian values to the underlying output
	 * stream.
	 *
	 * @param out underlying output stream
	 * @see OutputStream
	 */
	public LittleEndianDataOutputStream(OutputStream out)
	{
		super(out);
	}

		
	/**
	 * Writes a series of double values to the underlying output
	 * stream, each with the value 0.0
	 *
	 * @param size number of doubles to write
	 */
	public void fillDouble(int size) throws IOException
	{
		for(int i = 0; i < size; i++)
			writeDouble(0.0);
	}

	/**
	 * Writes a series of int values to the underlying output
	 * stream, each with the value 0.
	 *
	 * @param size number of ints to write
	 */
	public void fillInt(int size) throws IOException
	{
		for(int i = 0; i < size; i++)
			writeInt(0);
	}

	/**
	 * Writes a series of byte values to the underlying output
	 * stream, each with the value 0.
	 * 
	 * @param size number of bytes to write
	 */
	public void fillByte(int size) throws IOException
	{
		for(int i = 0; i < size; i++)
			writeByte(0);
	}

	/**
	 * Writes a double in little endian data format to the underlying
	 * output stream.
	 * 
	 * @param value double value to write
	 */
	public void writeDoubleLE(double value) throws IOException
	{
		long result = Double.doubleToLongBits(value);
		result = Long.reverseBytes(result);
		writeLong(result);
	}

	/**
	 * Writes a int in little endian data format to the underlying
	 * output stream.
	 * 
	 * @param value int value to write
	 */
	public void writeIntLE(int value) throws IOException
	{
		int result = Integer.reverseBytes(value);
		writeInt(result);
	}

	/**
	 * Writes a short in little endian data format to the underlying
	 * output stream.
	 * 
	 * @param value short value to write
	 */
	public void writeShortLE(short value) throws IOException
	{
		short result = Short.reverseBytes(value);
		writeShort(result);
	}

}
